/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdDatabaseModelingTemplateBo;
import com.comac.ins.isfd.domain.bo.query.IsfdDatabaseModelingTemplateQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelingTemplateVo;
import com.comac.ins.isfd.service.IIsfdDatabaseModelingTemplateService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

/**
 * 数据库模版管理
 *
 * @author wujialin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/databaseModelingTemplate")
public class IsfdDatabaseModelingTemplateController extends BaseController {

    private final IIsfdDatabaseModelingTemplateService isfdDatabaseModelingTemplateService;

    /**
     * 查询数据库建模模版列表
     */
    @SaCheckPermission("isfd:databaseModelingTemplate:list")
    @PostMapping("/list")
    public R<List<IsfdDatabaseModelingTemplateVo>> list(@RequestBody IsfdDatabaseModelingTemplateQueryBo bo) {
        List<IsfdDatabaseModelingTemplateVo> configList = isfdDatabaseModelingTemplateService.queryList(bo);
        return R.ok(configList);
    }

    /**
     * 新增/修改数据库建模模版
     */
    @SaCheckPermission("isfd:databaseModelingTemplate:add")
    @Log(title = "数据库建模模版", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/save")
    public R<Void> save(@RequestBody IsfdDatabaseModelingTemplateBo bo) {
        return toAjax(isfdDatabaseModelingTemplateService.save(bo));
    }

    /**
     * 获取数据库建模模版详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:databaseModelingTemplate:query")
    @GetMapping("/{id}")
    public R<IsfdDatabaseModelingTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                                     @PathVariable Long id) {
        return R.ok(isfdDatabaseModelingTemplateService.queryById(id));
    }

    /**
     * 删除数据库建模模版
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:databaseModelingTemplate:remove")
    @Log(title = "数据库建模模版删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(isfdDatabaseModelingTemplateService.deleteWithValidById(id, true));
    }

    /**
     * 导出数据库建模模版json
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:databaseModelingTemplate:json")
    @GetMapping("/out-json/{id}")
    public ResponseEntity<Resource> outJson(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) throws Exception {
        IsfdDatabaseModelingTemplateVo isfdDatabaseModelingTemplateVo = isfdDatabaseModelingTemplateService.queryById(id);
        Resource txtResource = isfdDatabaseModelingTemplateService.outJson(isfdDatabaseModelingTemplateVo);
        ContentDisposition contentDisposition = ContentDisposition.attachment()
            .filename("数据库建模模版.json", StandardCharsets.UTF_8)
            .build();
        return ResponseEntity.ok()
            .contentLength(txtResource.contentLength())
            .contentType(MediaType.APPLICATION_JSON)
            .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
            .body(txtResource);
    }
//
//    /**
//     * 导入数据库建模模版excel
//     */
//    @PostMapping(value = "/import-excel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public R<Integer> importExcelToMongo(@RequestParam("file") MultipartFile file,
//                                         @RequestParam("templateId") String templateId) throws Exception {
//        Integer size = isfdDatabaseModelingTemplateService.importExcelToMongo(file, templateId);
//        return R.ok("Excel 文件成功导入 MongoDB", size);
//    }
}
